<template>
  <div class="divBox">
    <el-card class="box-card">
      <el-form inline size="small" @submit.native.prevent>
        <el-form-item label="关键词：">
          <el-input v-model="listPram.keyword" placeholder="输入手机号或昵称" clearable class="selWidth"/>
        </el-form-item>
        <el-form-item label="地区:" prop="areaId">
          <el-cascader class="col"
                       placeholder="可输入地区名称进行搜索"
                       v-model="value"
                       :options="options"
                       clearable
                       :props="{ expandTrigger: 'hover' }"
                       filterable
                       @change="handleChange"></el-cascader>
        </el-form-item>
        <el-form-item label="时间范围">
          <el-date-picker
              v-model="value1"
              type="datetimerange"
              value-format="yyyy-MM-dd HH:mm:ss"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @change="handleDateChange">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click.native="getList">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
        <el-form-item style="margin-left:100px;">
          <span>所有粉丝数:{{dataStatistics.allCount}}</span>
          <span style="margin-left: 20px">新增粉丝数:{{dataStatistics.newCount}}</span>
        </el-form-item>
      </el-form>
      <el-table :data="listData.list" size="mini"
                :header-cell-style=" {fontWeight:'bold',background:'#f8f8f9',color: '#515a6e',height:'40px'}" border>
        <el-table-column label="ID" prop="id" width="120"></el-table-column>
        <el-table-column label="头像" prop="avatar">
          <template slot-scope="scope">
            <el-image :src="imageUrl + scope.row.faceUrl"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="昵称" prop="name">
          <template slot-scope="scope">
            <div> {{ scope.row.name || '-' }}</div>
          </template>
        </el-table-column>
        <el-table-column label="手机号" prop="phone"/>
        <el-table-column label="所在地区" prop="areaText"/>
        <el-table-column label="注册时间" prop="createTime"/>

        <el-table-column label="操作" min-width="180" fixed="right" align="center" width="400">
          <template slot-scope="scope">
            <el-button v-has-permi="['admin:user:userGrant']" icon="el-icon-document-remove" size="mini"
                       type="success"
                       @click="openUserGrant(scope.row.id,scope.row)">发放
            </el-button>
            <el-button icon="el-icon-s-order" size="mini" type="primary" @click="openUserEdit(scope.row.id)"
                       v-has-permi="['admin:user:detail']">用户详情
            </el-button>
            <el-button icon="el-icon-edit" size="mini" type="primary" @click="openEdit(scope.row.id)"
                       v-has-permi="['admin:user:detail']">记录详情
            </el-button>

          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          :current-page="listPram.page"
          :page-sizes="[20,40,60,80,100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listData.count"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </el-card>
    <el-dialog
        :visible.sync="editDialog.show"
        :title="editDialog.id? '用户详情':'新增'"
        destroy-on-close
        :close-on-click-modal="false"
        width="1200px">
      <edit
          v-if="editDialog.show"
          :id="editDialog.id"
          @hideEditDialog="hideEditDialog"
          ref="editForm"
      />
    </el-dialog>
    <el-dialog
        :close-on-click-modal="false"
        title="发放益联券"
        :visible.sync="userGrant.show"
        destroy-on-close
        top="80px"
        width="1200px">
      <UserGrant
          v-if="userGrant.show"
          :id="userGrant.id"
          :edit-data="userGrant.editDate"
          ref="editForm"
          @hideGrantDialog="hideUserGrantDialog"
      />
    </el-dialog>
    <el-dialog
        :visible.sync="editUserDialog.show"
        :title="editUserDialog.id? '用户详情':'新增'"
        destroy-on-close
        :close-on-click-modal="false"
        width="1000px">
      <userEdit
          v-if="editUserDialog.show"
          :id="editUserDialog.id"
          @hideUserEditDialog="hideUserEditDialog"
          ref="editForm"
      />
    </el-dialog>
  </div>
</template>

<script>
import edit from './edit'
import {deleteRow, list} from "@/api/user";
import {checkPermi} from "@/utils/permission";
import UserGrant from "@/views/user/fans/userGrant.vue";
import {optionsList, getUserAllCount} from "@/api/business";
import userEdit from "@/views/user/fans/userEdit.vue";

export default {
  components: {userEdit, UserGrant, edit},
  data() {
    return {
      value1:'',
      imageUrl: '',
      listData: {list: [], count: 0},
      listPram: {
        keyword: '',
        areaId: '',
        ifVip: 2,
        page: 1,
        limit: 20,
        beginTime:'',
        endTime:''
      },
      editDialog: {
        show: false,
        id: null
      },
      editUserDialog: {
        show: false,
        id: null
      },
      userGrant: {
        show: false,
        id: null,
        editDate: {},
        pid: null
      },
      tableLoading: false,
      value: [],
      options: [{
        value: '',
        label: '',
        children: [{
          value: '',
          label: '',
          children: [{
            value: '',
            label: ''
          }]
        }]
      }],
      dataStatistics: {newCount: 0, allCount: 0}
    }
  },
  mounted() {
    this.imageUrl = process.env.VUE_APP_IMAGE_URL
    this.getList()
    this.getOptions()
    this.getDataStatistics()
  },
  methods: {
    openUserEdit(id = null){
      this.editUserDialog.id = id;
      this.editUserDialog.show = true
    },
    getDataStatistics() {
      getUserAllCount(this.listPram).then(res => {

        this.dataStatistics = res

      })
    },
    hideUserEditDialog() {
      this.editUserDialog.show = false
      this.getList()
    },
    handleDateChange(value) {
      this.listPram.beginTime = value[0];
      this.listPram.endTime = value[1];
    },
    handleChange(value) {
      this.listPram.areaId = value[2]
    },
    getOptions() {
      optionsList().then(res => {
        this.options = res
      })
    },
    checkPermi,
    getList() {
      this.tableLoading = true
      list(this.listPram).then(res => {
        this.listData.list = res.list;
        this.listData.count = res.count;
      }).finally(() => {
        this.getDataStatistics()
      })
      this.tableLoading = false;
    },
    openEdit(id = null) {
      this.editDialog.id = id;
      this.editDialog.show = true
    },
    openUserGrant(id = null, editDate) {
      this.userGrant.id = id;
      this.userGrant.show = true
      this.userGrant.editDate = editDate
    },
    handlerDelete(id) {
      this.$confirm('确认删除该数据吗？').then(() => {
        const loading = this.$loading({text: '删除中', background: 'rgba(255,255,255,.5)'})
        deleteRow(id).then(() => {
          this.$message.success('删除成功')
          this.getList()
        }).finally(() => {
          loading.close()
        })
      })
    },
    hideEditDialog() {
      this.editDialog.show = false
      this.getList()
    },
    hideUserGrantDialog() {
      this.userGrant.show = false
      this.getList()
    },
    handleSizeChange(val) {
      this.listPram.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listPram.page = val
      this.getList()
    },
    // handleStatusChange(status,id){
    // const loading=this.$loading({text:'更新中',background:'rgba(255,255,255,.5)'})
    // updateStatus(id,status).then(()=>{
    //   this.$message.success('更新状态成功')
    //   this.getList()
    // }).finally(()=>{
    //   loading.close()
    // })
    // },
    resetQuery() {
      this.listPram.keyword = '';
      this.listPram.areaId = '';
      this.listPram.endTime = '';
      this.listPram.beginTime = '';
      this.value = ''
      this.getList()
    }
  }
}
</script>

<style scoped lang="scss">

</style>
